package com.collectplus.express.menu;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppConfig;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.CheckUpdateModel;
import com.collectplus.express.upgrade.UpgradeActivity;
import com.shouhuobao.bhi.login.GuildActivity;

import java.util.ArrayList;

/**
 * 关于我们
 * 
 * @author coffee <br>
 *         2016-1-9下午5:15:07
 */
public class AboutActivity extends BaseActivity2 implements OnClickListener {

	private final ArrayList<String> mArr = new ArrayList<String>();
	private ListView mListView;
	private AboutAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setData();
	}

	@Override
	protected void findViewById() {
		setContentView(R.layout.about_us);
		super.findViewById();
		getAppTitle().setCommonTitle("关于我们");
		TextView mCode = (TextView) findViewById(R.id.about_version_code);
		// 获取版本号
		try {
			PackageManager packageManager = getContext().getPackageManager();
			PackageInfo packInfo = packageManager.getPackageInfo(getContext().getPackageName(), 0);
			mCode.setText(getString(R.string.app_name) + " v" + packInfo.versionName);
			if (AppConfig.isTest == true) {
				mCode.append(" 测试版");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		mListView = (ListView) findViewById(R.id.about_listview);
		// 隐藏标题与内容之间的分割线
		mAdapter = new AboutAdapter(mArr, this);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				switch (position) {
				case 0:
					// 引导界面
					Intent intent = new Intent();
					intent.setClass(getContext(), GuildActivity.class);
					startActivity(intent);
					break;
				case 1:
					try {
						Uri uri = Uri.parse("market://details?id=" + getPackageName());
						intent = new Intent(Intent.ACTION_VIEW, uri);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent);
					} catch (Exception e) {
						showToast("未发现应用市场");
						// e.printStackTrace();
					} catch (Error e) {
						showToast("未发现应用市场");
						// e.printStackTrace();
					}
					break;
				case 2:
					Intent i = new Intent(getContext(), WebViewActivity.class);
					i.putExtra("title", "法律条款");
					startActivity(i);
					break;
				case 3:
					showLoadingDialog(null);
					AppHttp.getInstance().checkUpdate();
					break;
				}
			}
		});
	}

	private void setData() {
		mArr.add("新功能介绍");
		mArr.add("为火箭投递评分");
		// mArr.add("推荐给好友");
		mArr.add("法律条款");
		mArr.add("检查更新");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		}
	}

	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case MsgID.checkUpdate:
			cancelLoadingDialog();
			AppResult<CheckUpdateModel> result = JSON.parser(msg.obj.toString(), CheckUpdateModel.class);
			if (result == null) {
				return true;
			}
			final CheckUpdateModel item = result.getResult();
			if (item == null) {
				return true;
			}
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					// 强制升级 -- 非强制升级
					if (item.getIsNeedUpdate() == 2 || item.getIsNeedUpdate() == 1) {
						Intent intent = new Intent();
						intent.putExtra("item", item);
						intent.setClass(getContext(), UpgradeActivity.class);
						startActivity(intent);
					} else if (item.getIsNeedUpdate() == 0) {// 最新版本
						showToast("当前版本已经是最新版本");
					}
				}
			});
			return true;
		}
		return false;
	}

}
